import functools
from flask import session, g, current_app


def click_news_filter(index):
    if index == 1:
        return 'first'
    elif index == 2:
        return "second"
    elif index == 3:
        return "third"
    else:
        return ""


# 定义验证是否登陆的装饰器
def user_login_data(f):

    @functools.wraps(f)
    def wrapper(*args, **kwargs):
        # 获取到当前登录用户的id
        user_id = session.get('user_id')
        # 通过id获取用户信息
        user = None
        if user_id:
            from Home.models import User
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)

        g.user = user
        return f(*args, **kwargs)
    return wrapper


def down_load_file(data):
    import os
    file_name=os.path.dirname(__file__)
    with open('', 'wb') as f:
        f.write(data)
